﻿/*
 * Ext Gantt 1.2
 * Copyright(c) 2009-2011 Mats Bryntse Consulting
 * mats@ext-scheduler.com
 * http://www.ext-scheduler.com/license.html
 *
 */

Ext.ns('Sch.plugins');

/**
 * @class Sch.gantt.plugins.DependencyEditor
 * @extends Ext.form.FormPanel
 * @requires Ext.ux.form.SpinnerField
 * A FormPanel used to edit dependencies
 * @constructor
 * Create a new instance of this plugin
 * @param {Object} config The configuration options
 */
Sch.gantt.plugins.DependencyEditor = Ext.extend(Ext.form.FormPanel, {
    
    /**
     * @cfg {String} hoursText The text to show after the hour spinner field
     */
    hoursText : 'hrs',
    
    /**
     * @cfg {Boolean} hideOnBlur True to hide this panel if a click is detected outside the panel (defaults to true)
     */
    hideOnBlur : true,
    
    /**
     * @cfg {String} fromText The text to before the From label
     */
    fromText : 'From',
    
    /**
     * @cfg {String} toText The text to before the To label
     */
    toText : 'To',

    /**
     * @cfg {String} typeText The text to before the Type field
     */
    typeText : 'Type',

    /**
     * @cfg {String} lagText The text to before the Lag field
     */
    lagText : 'Lag',

    /**
     * @cfg {String} endToStartText 
     */
    endToStartText : 'Finish-To-Start',

    /**
     * @cfg {String} startToStartText 
     */
    startToStartText : 'Start-To-Start',

    /**
     * @cfg {String} endToEndText 
     */
    endToEndText : 'Finish-To-Finish',

    /**
     * @cfg {String} startToEndText 
     */
    startToEndText : 'Start-To-Finish',
       
    /**
     * @cfg {Boolean} showLag True to show the lag editor
     */
    showLag : false,

    /* 
     * Expands the editor
     * @param {Record} dependencyRecord The record to show in the editor panel
     * @param {Array} xy the coordinates where the window should be shown
     */
    show : function(dependencyRecord, xy) {
        this.dependencyRecord = dependencyRecord;
        
        // Load form panel fields
        this.getForm().loadRecord(dependencyRecord);
        this.fromLabel.setValue(this.taskStore.getById(this.dependencyRecord.get('From')).get('Name'));
        this.toLabel.setValue(this.taskStore.getById(this.dependencyRecord.get('To')).get('Name'));

        this.el.setXY(xy);
        this.expand();
    },
    
    border : false,
    height: 130,
    width : 260,
    frame : true,
    labelWidth : 60,
    header : false,

    initComponent : function() {
        Ext.apply(this, {
            items : this.buildFields()
        });
        Sch.gantt.plugins.DependencyEditor.superclass.initComponent.call(this);
    },

    buildFields : function() {
        var me = this,
            sd = Sch.gantt.Dependency,
            fs = [
            this.fromLabel = new Ext.form.DisplayField({
                fieldLabel : this.fromText
            }),
                    
            this.toLabel = new Ext.form.DisplayField({
                fieldLabel : this.toText
            }),

            this.typeField = new Ext.form.ComboBox({
                name : 'Type',
                fieldLabel : this.typeText,
                triggerAction : 'all',
                mode : 'local',
                valueField : 'value',
                displayField : 'text',
                editable : false,
                width : 170,
                store : new Ext.data.JsonStore({
                    fields : ['text', 'value'],
                    data : [{
                        text : this.endToStartText,
                        value : sd.EndToStart
                    },
                    {
                        text : this.startToStartText,
                        value : sd.StartToStart
                    },
                    {
                        text : this.endToEndText,
                        value : sd.EndToEnd
                    },
                    {
                        text : this.startToEndText,
                        value : sd.StartToEnd
                    }]
                })
            })
        ];

        if (this.showLag) {
            fs.push(
                this.lagField = new Ext.ux.form.SpinnerField({
                    width : 50,
                    name : 'Lag',
                    fieldLabel : this.lagText
                })
            );
        }

        return fs;
    },
    
    init : function(cmp) {
        cmp.on('dependencydblclick', this.onDependencyDblClick, this);
        cmp.on('render', this.onGanttRender, this);
        
        this.gantt = cmp;
        this.taskStore = cmp.getTaskStore();
    },
    
    onDependencyDblClick : function(g, record, e, t) {
        this.show(record, e.getXY());
    },
    
    onGanttRender : function() {
        this.render(Ext.getBody());

        this.el.addClass('sch-gantt-dependencyeditor');

        // Collapse after render, otherwise rendering is messed up
        this.collapse();
         
        if (this.hideOnBlur) {
            // Hide when clicking outside panel
            this.mon(Ext.getBody(), 'click', this.onMouseClick, this);
        }
    },

    onMouseClick : function(e){
        var tf = this.typeField;
        
        if (!this.collapsed && 
            !e.within(this.getEl()) &&
            (!tf.view || !e.within(tf.view.el))) {
            this.collapse(false);
        }
    }
});
